<?php

/**
 * tasaConv actions.
 *
 * @package    aeropostal
 * @subpackage tasaConv
 * @author     Your name here
 */
class tasaConvActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    // Se define la cantidad de resultados que se quieren por pagina
	$pager = new sfPropelPager('TasaConv', sfConfig::get('app_max_tasascamb'));
	$pager->setCriteria(new Criteria());
	// Se establece que la pagina principal del index sera la 1
	$pager->setPage($this->getRequestParameter('page', 1));
	$pager->init();
	 // Se Prepara la informaci�n para usarla en la vista.
	$this->pager = $pager;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->TasaConv = TasaconvPeer::retrieveByPk($request->getParameter('umedida1'),
                                 $request->getParameter('umedida2'));
    $this->forward404Unless($this->TasaConv);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new TasaConvForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new TasaConvForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($TasaConv = TasaconvPeer::retrieveByPk($request->getParameter('umedida1'),
                           $request->getParameter('umedida2')), sprintf('Object TasaConv does not exist (%s).', $request->getParameter('umedida1'),
                           $request->getParameter('umedida2')));
    $this->form = new TasaConvForm($TasaConv);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($TasaConv = TasaconvPeer::retrieveByPk($request->getParameter('umedida1'),
                           $request->getParameter('umedida2')), sprintf('Object TasaConv does not exist (%s).', $request->getParameter('umedida1'),
                           $request->getParameter('umedida2')));
    $this->form = new TasaConvForm($TasaConv);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($TasaConv = TasaconvPeer::retrieveByPk($request->getParameter('umedida1'),
                           $request->getParameter('umedida2')), sprintf('Object TasaConv does not exist (%s).', $request->getParameter('umedida1'),
                           $request->getParameter('umedida2')));
    $TasaConv->delete();
    $numero = $request->getParameter('umedida1').'-'.$request->getParameter('umedida2');
    LogPeer::logBorrar($numero,$this->getUser(),'TasaConv');
    $this->redirect('tasaConv/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        try {
            $TasaConv = $form->save();
            $numero = $request->getPostParameter('tasaconv[uMedida1]').'-'.$request->getPostParameter('tasaconv[uMedida2]');
            LogPeer::log($numero,$this->getUser(),'TasaConv');
            $this->redirect('tasaConv/index');
        }catch (Exception $e){
            $this->getUser()->setFlash('error', 'No se puede registrar este Factor de conversión, ya existe para la unidades indicadas.');
            return sfView::NONE;
        }
    }
  }
}
